package edu.ccut.saturn.basecomponent.database.facade;

import java.sql.Connection;
import java.util.List;

import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.basecomponent.database.SaturnSQLHandler;
import edu.ccut.saturn.basecomponent.database.impl.SaturnSQL;
import edu.ccut.saturn.basecomponent.database.util.SaturnDataUtil;
import edu.ccut.saturn.component.SaturnData;

public class QueryBySQL {
	public static List<SaturnData> execute(String fullKey, String sql, Connection conn)
			throws Exception {
		SaturnSQL saturnSQL = new SaturnSQL();
		String dbType = SaturnDataUtil.getStandardDBTypeByConn(conn);
		saturnSQL.setDBtype(dbType);
		saturnSQL.setOperationType(getBuilderType(fullKey));
		saturnSQL.setSaturnDataFullKey((String) fullKey);
		saturnSQL.setSql(sql);
		List<SaturnData> list = SaturnSQLHandler.executeQuery(saturnSQL,conn);
		return list;
	}
	public static List<SaturnData> execute(String fullKey, String sql,String dbType, Connection conn)
	throws Exception {
		SaturnSQL saturnSQL = new SaturnSQL();
		saturnSQL.setDBtype(dbType);
		saturnSQL.setOperationType(getBuilderType(fullKey));
		saturnSQL.setSaturnDataFullKey((String) fullKey);
		saturnSQL.setSql(sql);
		List<SaturnData> list = SaturnSQLHandler.executeQuery(saturnSQL,conn);
		return list;
	}

	private static String getBuilderType(String fullKey) {
		// 如果有大数据
		if (hasLOB(fullKey)) {
			return ISaturnSQL.SELECT_LOB_OPERATION;
		} else {
			return ISaturnSQL.SELECT_OPERATION;
		}
	}

	private static boolean hasLOB(String fullKey) {
		return SaturnDataUtil.hasLOB(fullKey);
	}
}
